package com.itheima.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.AccountDao;
import com.itheima.domain.Account;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.awt.print.Paper;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class AccountDemo {
    private SqlSession session;
    private AccountDao mapper;

    @Before
    public void init() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("MybaitsConfig.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        session = sessionFactory.openSession();

        mapper = session.getMapper(AccountDao.class);
    }

    @After
    public void close(){
        session.close();
    }


    @Test
    public void findById() throws Exception {
        Account byId = mapper.findById(1);

        System.out.println("byId = " + byId);

    }

    @Test
    public void findByName() throws Exception {
        List<Account> accountList = mapper.findByName("程");

        for (Account account : accountList) {
            System.out.println(account);
        }
    }

    @Test
    public void findByName2() throws Exception {
        List<Account> accountList = mapper.findByName2("程");

        for (Account account : accountList) {
            System.out.println(account);
        }
    }

    @Test
    public void findAllByLimet() throws Exception {
        PageHelper.startPage(1,2);
        List<Account> accountList = mapper.findAll();

        //封装PageInfo对象
        PageInfo<Account> accounts = new PageInfo<>(accountList);
        System.out.println("总页数");
        System.out.println("总条数");
        List<Account> accountsList = accounts.getList();
        for (Account account : accountList) {
            System.out.println(account);
        }

    }
}
